<template>
  <div class="log-upload">
    <!-- 进度条和提示在上方 -->
    <div v-if="uploading" class="upload-progress">
      <div class="progress-bar">
        <div class="progress-fill" :style="{ width: progress + '%' }"></div>
      </div>
      <p>正在上传和解析文件...</p>
    </div>
    
    <transition name="result-fade" appear>
      <div 
        v-if="uploadResult" 
        class="upload-result" 
        :class="{ success: uploadResult.success, error: !uploadResult.success }"
        @click="hideResult"
      >
      <div v-if="uploadResult.success" class="success-message">
        <div class="success-icon">✅</div>
        <div class="success-content">
          <div class="success-title">{{ uploadResult.message }}</div>
          <div class="upload-details">
            <div v-if="uploadResult.vector_update" class="detail-item">
              <span class="detail-label">向量记录:</span>
              <span class="detail-value">{{ uploadResult.vector_update.added_documents }} 条已添加</span>
            </div>
          </div>
        </div>
        <div class="close-hint">点击关闭</div>
      </div>
      <div v-else class="error-message" @click="hideResult">
        <div class="error-icon">❌</div>
        <div class="error-content">
          <div class="error-title">{{ uploadResult.message }}</div>
        </div>
        <div class="close-hint">点击关闭</div>
      </div>
      </div>
    </transition>
    
    <!-- 拖拽区域在下方 -->
    <div class="upload-area" @click="triggerFileInput" @dragover.prevent @drop.prevent="handleDrop">
      <input
        ref="fileInput"
        type="file"
        accept=".log,.txt,.csv,.json"
        @change="handleFileSelect"
        style="display: none"
      />
      <div class="upload-content">
        <div class="upload-icon">📁</div>
        <div class="upload-text">
          <p>点击或拖拽上传日志文件</p>
          <p class="upload-hint">支持 .log, .txt, .csv, .json 格式</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import api from '../api';

const emit = defineEmits(['upload-success', 'upload-error']);

const fileInput = ref(null);
const uploading = ref(false);
const progress = ref(0);
const uploadResult = ref(null);

const triggerFileInput = () => {
  fileInput.value.click();
};

const handleFileSelect = (event) => {
  const file = event.target.files[0];
  if (file) {
    uploadFile(file);
  }
};

const handleDrop = (event) => {
  event.preventDefault();
  const files = event.dataTransfer.files;
  if (files.length > 0) {
    uploadFile(files[0]);
  }
};

const uploadFile = async (file) => {
  // 验证文件类型
  const allowedTypes = ['.log', '.txt', '.csv', '.json'];
  const fileExtension = '.' + file.name.split('.').pop().toLowerCase();
  
  if (!allowedTypes.includes(fileExtension)) {
    uploadResult.value = {
      success: false,
      message: `不支持的文件类型: ${fileExtension}。支持的类型: ${allowedTypes.join(', ')}`
    };
    emit('upload-error', uploadResult.value);
    return;
  }
  
  // 验证文件大小 (10MB)
  const maxSize = 10 * 1024 * 1024;
  if (file.size > maxSize) {
    uploadResult.value = {
      success: false,
      message: '文件大小不能超过10MB'
    };
    emit('upload-error', uploadResult.value);
    return;
  }
  
  try {
    uploading.value = true;
    progress.value = 0;
    uploadResult.value = null;
    
    // 模拟进度
    const progressInterval = setInterval(() => {
      if (progress.value < 90) {
        progress.value += 10;
      }
    }, 200);
    
    const response = await api.uploadLogFile(file);
    
    clearInterval(progressInterval);
    progress.value = 100;
    
    uploadResult.value = {
      success: true,
      message: response.data.message,
      filename: response.data.filename,
      vector_update: response.data.vector_update
    };
    
    emit('upload-success', uploadResult.value);
    
    // 移除自动清除，只有点击才消失
    
  } catch (error) {
    clearInterval(progressInterval);
    uploadResult.value = {
      success: false,
      message: error.response?.data?.error || '上传失败，请重试'
    };
    emit('upload-error', uploadResult.value);
  } finally {
    uploading.value = false;
    progress.value = 0;
  }
};

const hideResult = () => {
  uploadResult.value = null;
};
</script>

<style scoped>
.log-upload {
  margin-bottom: 0.1rem;
}

.upload-area {
  border: 2px dashed var(--border-color);
  border-radius: var(--radius);
  padding: 1rem 1rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  background: linear-gradient(135deg, var(--bg-color), rgba(255, 255, 255, 0.02));
  position: relative;
  overflow: hidden;
}

.upload-area::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, var(--primary-color), rgba(59, 130, 246, 0.1));
  opacity: 0;
  transition: opacity 0.3s ease;
}

.upload-area:hover {
  border-color: var(--primary-color);
  background: linear-gradient(135deg, var(--card-bg), rgba(255, 255, 255, 0.05));
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.upload-area:hover::before {
  opacity: 0.05;
}

.upload-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  position: relative;
  z-index: 1;
}

.upload-icon {
  font-size: 1.5rem;
  margin-bottom: 0.25rem;
  transition: transform 0.3s ease;
}

.upload-area:hover .upload-icon {
  transform: scale(1.1);
}

.upload-text p {
  margin: 0;
  color: var(--text-color);
  transition: color 0.3s ease;
  font-size: 0.9rem;
}

.upload-area:hover .upload-text p {
  color: var(--primary-color);
}

.upload-hint {
  font-size: 0.75rem;
  color: var(--text-secondary);
  transition: color 0.3s ease;
}

.upload-area:hover .upload-hint {
  color: var(--text-color);
}

.upload-progress {
  margin-bottom: 0.75rem;
  padding: 0.75rem;
  background: linear-gradient(135deg, var(--card-bg), rgba(255, 255, 255, 0.02));
  border-radius: var(--radius);
  border: 1px solid var(--border-color);
}

.progress-bar {
  width: 100%;
  height: 6px;
  background: linear-gradient(90deg, var(--border-color), rgba(255, 255, 255, 0.1));
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 0.5rem;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary-color), #60a5fa);
  transition: width 0.3s ease;
  position: relative;
}

.progress-fill::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

.upload-progress p {
  margin: 0;
  text-align: center;
  color: var(--text-color);
  font-weight: 500;
  font-size: 0.75rem;
}

.upload-result {
  margin-bottom: 0.75rem;
  padding: 0.5rem;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.upload-result::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.upload-result:hover::before {
  left: 100%;
}

.upload-result.success {
  background: linear-gradient(135deg, rgba(34, 197, 94, 0.1), rgba(34, 197, 94, 0.05));
  border: 1px solid rgba(34, 197, 94, 0.3);
  color: #166534;
  box-shadow: 0 2px 8px rgba(34, 197, 94, 0.1);
}

.upload-result.success:hover {
  background: linear-gradient(135deg, rgba(34, 197, 94, 0.15), rgba(34, 197, 94, 0.08));
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(34, 197, 94, 0.2);
}

.upload-result.error {
  background: linear-gradient(135deg, rgba(239, 68, 68, 0.1), rgba(239, 68, 68, 0.05));
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: #dc2626;
  box-shadow: 0 2px 8px rgba(239, 68, 68, 0.1);
}

.upload-result.error:hover {
  background: linear-gradient(135deg, rgba(239, 68, 68, 0.15), rgba(239, 68, 68, 0.08));
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.2);
}

.success-message, .error-message {
  display: flex;
  align-items: flex-start;
  gap: 0.375rem;
}

.success-icon, .error-icon {
  font-size: 1rem;
  flex-shrink: 0;
  margin-top: 0.125rem;
}

.success-content, .error-content {
  flex: 1;
}

.success-title, .error-title {
  font-weight: 600;
  margin-bottom: 0.25rem;
  font-size: 0.875rem;
}

.upload-details {
  margin-top: 0.375rem;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.detail-item {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 0.25rem;
  font-size: 0.7rem;
  text-align: left;
}

.detail-label {
  font-weight: 500;
  opacity: 0.8;
  min-width: 45px;
}

.detail-value {
  font-weight: 600;
  color: var(--primary-color);
}

.close-hint {
  position: absolute;
  top: 0.25rem;
  right: 0.5rem;
  font-size: 0.625rem;
  opacity: 0.6;
  background: rgba(255, 255, 255, 0.8);
  padding: 0.125rem 0.375rem;
  border-radius: 0.25rem;
  backdrop-filter: blur(4px);
}

/* 动画效果 */
.result-fade-enter-active {
  transition: all 0.5s ease;
}

.result-fade-enter-from {
  opacity: 0;
  transform: translateY(20px) scale(0.95);
}

.result-fade-leave-active {
  transition: all 0.3s ease;
}

.result-fade-leave-to {
  opacity: 0;
  transform: translateY(-10px) scale(0.95);
}

/* 成功提示的特殊动画 */
.upload-result.success {
  animation: successPulse 0.6s ease-out;
}

@keyframes successPulse {
  0% {
    transform: scale(0.9);
    opacity: 0;
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}
</style>
